package com.kuangstudy.controller.blog;

import com.kuangstudy.entity.Blog;
import com.kuangstudy.service.blog.IBlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @author zhangfushuai
 * @description 文章明细数据
 * @since 2022/4/17 18:22
 **/
@Controller
@Slf4j
public class BlogDetailController {

    @Autowired
    private IBlogService blogService;

    /**
     * @author zhangfushuai
     * @description http://localhost:8888/detail/1 根据文章id，跳转到文章明细页面
     * @since 2022/4/17 18:22
     * 参数 blogId
     * 参数 session
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @GetMapping("/detail/{blogId}")
    public ModelAndView blogdetail(@PathVariable("blogId") Integer blogId, HttpSession session) {
        // 1； 定义数据模型
        ModelAndView modelAndView = new ModelAndView();
        // 5: 根据id查询
        Blog blog = blogService.getById(blogId);
        // 6: 把数据放入数据模型中
        modelAndView.addObject("blog", blog);
        // 7: 把视图放入进去 ,注意：这里blog前面千万不要加/否则找不到页面
        modelAndView.setViewName("/blog/detail");
        return modelAndView;
    }
}
